<?php


class BlockAction extends PublicAction {

    protected $db,$Type;

    function _initialize()
    {
        parent::_initialize();
        $this->db = M('Block');
        $this->Type = F('Type');
        $pos = !empty($_REQUEST['pos'])?$_REQUEST['pos']:'';

    }


    public function index()
    {
        if(APP_LANG)
            $map['lang']=array('eq',LANG_ID);

        $modelname = MODULE_NAME;
        $sortBy = '';
        $asc = false;
        $listRows = 15;

        $model = M($modelname);
        $id = $model->getPk();
        $this->assign('pkid', $id );
        if (isset($_REQUEST['order'])) {
            $order = $_REQUEST ['order'];
        } else {
            $order = !empty($sortBy) ? $sortBy : $id;
        }

        if (isset($_REQUEST['sort'])) {
            $_REQUEST['sort']=='asc' ? $sort = 'asc' : $sort = 'desc';
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }

        $map['lang'] = array('eq',LANG_ID);

        $tables = $model->getDbFields();

        $this->assign($_REQUEST);

        //取得满足条件的记录总数
        $count = $model->where($map)->count($id);//echo $model->getLastsql();

        if ($count > 0) {
            import("@.ORG.Page");
            //创建分页对象
            if (! empty($_REQUEST ['listRows'])) {
                $listRows = $_REQUEST ['listRows'];
            }

            $p = new Page($count, $listRows);

            //分页查询数据
            $voList = $model->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );

            //分页跳转的时候保证查询条件
            foreach ( $map as $key => $val ) {
                if (! is_array ( $val )) {
                    $p->parameter .= "$key=" . urlencode ( $val ) . "&";
                }
            }
            $map[C('VAR_PAGE')]='{$page}';

            $page->urlrule = U($modelname.'/index', $map);
            //分页显示
            $page = $p->show();

            //模板赋值显示
            $this->assign('list', $voList );
            $this->assign('page', $page );
        }

        $this->display();
    }


    public function edit()
    {
        if (IS_POST) {
            $model = D('Block');

            if (false === $model->create()) {
                $this->error($model->getError ());
            }

            if (false !== $model->save ()) {



                if($_POST['isajax'])
                    $this->assign('dialog','1');

                $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U('Block/index');

                $this->assign('jumpUrl', $jumpUrl);
                $this->success(L('edit_ok'));
            } else {
                $this->success (L('edit_error').': '.$model->getDbError());
            }
        } else {

            $pos = strip_tags($_REQUEST['pos']);
            $model = M('Block');
            $pk = ucfirst($model->getPk());
            $id = $_REQUEST[$model->getPk()];

            if(empty($id))
                $this->error(L('do_empty'));

            if($pos){
                $map['pos']=array('eq',$pos);
                if(APP_LANG)$map['lang']=array('eq',LANG_ID);

                $vo = $model->where($map)->find();
            }else{
                $do='getBy'.$pk;
                $vo = $model->$do ( $id );
            }

            if($vo['setup'])
                $vo['setup']=string2array($vo['setup']);

            $this->assign('vo', $vo);
            $this->display();
        }
    }


    function add()
    {
        if (IS_POST) {
            $model = D('Block');

            $_POST['lang'] = LANG_ID;
            if (false === $model->create ()) {
                $this->error( $model->getError () );
            }

            $id = $model->add();

            if ($id !==false) {

                $jumpUrl = U('Block/index');

                $this->assign ( 'jumpUrl',$jumpUrl );
                $this->success (L('add_ok'));
            } else {
                $this->error (L('add_error').': '.$model->getDbError());
            }
        } else {
            $this->display();
        }
    }


    function delete()
    {

        $Block = M('Block');
        $pk = $Block->getPk();
        $id = $_REQUEST [$pk];

        if (isset( $id )) {

            if(false!==$Block->delete($id)){
                delattach(array('moduleid'=>'231','id'=>$id));
                $this->assign( 'jumpUrl', U('Block/index') );
                $this->success(L('delete_ok'));
            }else{
                $this->error(L('delete_error').': '.$Block->getDbError());
            }
        }else{
            $this->error (L('do_empty'));
        }
    }
}